﻿''' <summary>
''' Encapsulates a set of LR(1) Items, used as a state in the LR(1) DPDA
''' </summary>
Public NotInheritable Class LR1State

    Private ReadOnly m_items As IEnumerable(Of LR1Item)
    Public ReadOnly Property Items() As IEnumerable(Of LR1Item)
        Get
            Return m_items
        End Get
    End Property


    Public Sub New(ByVal items As IEnumerable(Of LR1Item))

        Contract.ArgumentNotNull(items, "items")

        m_items = items
    End Sub

    Public Overrides Function Equals(ByVal obj As Object) As Boolean
        Dim other As LR1State = TryCast(obj, LR1State)
        If other Is Nothing Then Return False

        Dim selfSet As HashSet(Of LR1Item) = TryCast(Items, HashSet(Of LR1Item))
        If selfSet Is Nothing Then
            selfSet = New HashSet(Of LR1Item)(Items)
        End If

        Return selfSet.SetEquals(other.Items)
    End Function

    Public Overrides Function GetHashCode() As Integer
        Dim result As Integer = 0
        For Each item In m_items
            result = result Xor item.GetHashCode
        Next
        Return result
    End Function

End Class

